package com.wangzz.mallchat.common.user.service.adapter;

import com.wangzz.mallchat.common.user.domain.entity.User;
import com.wangzz.mallchat.common.websocket.domain.enums.WSRespTypeEnum;
import com.wangzz.mallchat.common.websocket.domain.vo.resp.WSBaseResp;
import com.wangzz.mallchat.common.websocket.domain.vo.resp.WSLoginSuccess;
import com.wangzz.mallchat.common.websocket.domain.vo.resp.WSLoginUrl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

public class WebSocketAdapter {

    /**
     * 处理点击登录响应二维码
     *
     * @param wxMpQrCodeTicket
     * @return
     */
    public static WSBaseResp<?> buildResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        // 3.封装返回
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        resp.setData(new WSLoginUrl(wxMpQrCodeTicket.getUrl()));
        return resp;
    }

    /**
     * 登录成功返回用户信息 / 认证成功返回用户信息
     *
     * @param user
     * @param token
     * @return
     */
    public static WSBaseResp<?> buildResp(User user, String token) {
        WSBaseResp<WSLoginSuccess> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        // 封装返回信息
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .token(token)
                .avatar(user.getAvatar())
                .uid(user.getId())
                .name(user.getName())
                .build();
        resp.setData(wsLoginSuccess);
        return resp;
    }

    /**
     * 构建等待授权返回
     *
     * @return
     */
    public static WSBaseResp<?> buildAuthorizeResp() {
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return resp;
    }

    /**
     * 推送token失效，前端删除token
     *
     * @return
     */
    public static WSBaseResp<?> buildInvalidTokenResp() {
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return resp;
    }
}
